package javacode.chapter07;

public class Example7_6 {
    public static void main(String[] args) {
        try {
            Teacher teacher = new Teacher();
            teacher.setName("小小老师");
            teacher.setAge(100);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            System.out.println("程序运行结束！");
        }
    }
}

class MyException extends Exception{
    public MyException(String exceptionMessage){
        super(exceptionMessage);
    }

    public MyException(){
        super();
    }

}

class Teacher{
    private String name;
    private int age;

    public void setName(String name){
        this.name=name;
    }

    public String getName(){
        return this.name;
    }

    public void setAge(int age) throws Exception{
        if (age>=0 && age<=100){
            this.age=age;
        }else {
            throw new MyException("年龄非法！");
        }
    }

    public int getAge(){
        return this.age;
    }
}
